require 'Qt4'

class TargetTableEntry
	attr_accessor :row
	attr_accessor :target
	attr_accessor :tasks
	
	def decreaseIfBigger(row)
		if (@row>row)
			@row=@row-1
		end
	end
	def initialize(target)
		@target=target
		@row=0
		@tasks=Array.new
	end
	
	def getTarget()
		return @target
	end
	
	def getRow()
		return @row
	end
	def setRow(row)
		@row=row
	end
end


class TaskTableEntry
	attr_accessor :row
	attr_accessor :task
	
	def decreaseIfBigger(row)
		if (@row>row)
			@row=@row-1
		end
	end
	
	def initialize(task)
		@task=task
		@row=0
	end
	
	def getTarget()
		return @task
	end
	
	def getRow()
		return @row
	end
	def setRow(row)
		@row=row
	end
end

class DatabaseTableSlice
	attr_accessor :rootTasks
	attr_accessor :targets
	
	def initialize()
		@rootTasks=Array.new
		@tasks=Array.new
		@targets=Array.new
	end
	def addTask(db,task)
		if task.getParent() == 0
			@rootTasks << TaskTableEntry(task)
		else
		    index = @targets.index { |x| x.target.getID()==task.getParent() }
			if index != nil
				@targets[index].tasks << TaskTableEntry.new(task)
			else
				target_entry = TargetTableEntry.new(db.getTarget(task.getParent()))
				target_entry.tasks << TaskTableEntry.new(task)
				@targets << target_entry
			end
		end
	end
	
	def enumerateTable(table)
		maxRow=0
		@rootTasks.each{|x|  
		    x.row=maxRow
		    maxRow=maxRow+1
			table.insertTask(x.row,x.task)
		}
		@targets.each{ |x| 
		    x.row=maxRow
		    maxRow=maxRow+1
			table.insertTarget(x.row,x.target)
			
		    x.tasks.each{ |y|
			  y.row=maxRow
			  maxRow=maxRow+1
			  table.insertTask(y.row,y.task)
		    }
		}
	end
	
	def findTaskByRow(row)
		index = @rootTasks.index{ |x| x.row==row }
		task = nil
		if index==nil
		    lindex= nil
			findex = @targets.index { |x|
			  lindex = x.tasks.index { |y| y.row==row }
			  lindex != nil
			}
			# If noting is found - exit
			if lindex ==nil
			  return nil
			end
			
			task=@targets[findex].tasks[lindex].task
		else
			task=@rootTasks[index].task
		end
		return task
	end
	
	def changeTaskStatus(form,row, see_all, new_status)
		task = self.findTaskByRow(row)
		if (task == nil)
			return
		end
		task.setStatus(new_status)
		if (see_all == true) or (new_status==true)
			form.updateTaskStatus(row,new_status)
		else
		   self.deleteTaskByRow(row,form)
		end
	end
	
	def findTask(row)
		index = @rootTasks.index{ |x| x.row==row}
		if index !=nil
			return [-1,index]
		else
			lindex=nil
			findex= @targets.index{ |x|
			  lindex = x.tasks.index { |y| y.row==row}
			  lindex != nil
			}
			return [findex,lindex]
		end
	end
	
	# Row must be valid task row
	def deleteTaskByRow(row,form) 
		taskpos=self.findTask(row)
		form.deleteRow(row)
		self.decreaseTableRowsBiggerThan(row)
		if (taskpos[0]==-1)
			@rootTasks.delete_at(taskpos[1])
		else
			@targets[taskpos[0]].tasks.delete_at(taskpos[1])
			if @targets[taskpos[0]].tasks.count()==0
				form.deleteRow(@targets[taskpos[0]].row)
				self.decreaseTableRowsBiggerThan(@targets[taskpos[0]].row)
				@targets.delete_at(taskpos[0])
			end
		end
	end
	
	def decreaseTableRowsBiggerThan(row)
		@rootTasks.each{|x|  
			if (x.row>row) 
				x.decreaseIfBigger(row)
			end
		
		}
		
		@targets.each{ |x|
			if (x.row>row)
				x.decreaseIfBigger(row)
			end
			
			x.tasks.each{ |y|
			  y.decreaseIfBigger(row)
		    }
		}
	end
end
